#构建后操作 增加 server3 openssh   Send files or execute commands over SSH before the build starts
#以后命令是在宿主机上执行的
mkdir -p   /home/yangxy/workspace/java-admin/xingchen-admin/target;
docker cp  jenkins:/var/jenkins_home/jobs/java-admin/workspace/docker/jenkins_java-adminAfter.sh  /home/yangxy/workspace/java-admin/xingchen-admin/target/jenkins_java-adminAfter.sh
docker cp  jenkins:/var/jenkins_home/jobs/java-admin/workspace/docker/jenkins_java-adminBefore.sh  /home/yangxy/workspace/java-admin/xingchen-admin/target/jenkins_java-adminBefore.sh
docker cp  jenkins:/var/jenkins_home/jobs/java-admin/workspace/docker/Dockerfile  /home/yangxy/workspace/java-admin/xingchen-admin/target/Dockerfile
docker cp  jenkins:/var/jenkins_home/jobs/java-admin/workspace/xingchen-admin/target/java-admin.jar  /home/yangxy/workspace/java-admin/xingchen-admin/target/java-admin.jar


#mkdir -p /home/yangxy/workspace/java-admin/xingchen-admin/target;
#docker cp  jenkins:/var/jenkins_home/jobs/java-admin/workspace/docker/jenkins_java-adminAfter.sh  /home/yangxy/workspace/java-admin/xingchen-admin/target/jenkins_java-adminAfter.sh
#docker cp  jenkins:/var/jenkins_home/jobs/java-admin/workspace/docker/jenkins_java-adminBefore.sh  /home/yangxy/workspace/java-admin/xingchen-admin/target/jenkins_java-adminBefore.sh
#docker cp  jenkins:/var/jenkins_home/jobs/java-admin/workspace/docker/Dockerfile  /home/yangxy/workspace/java-admin/xingchen-admin/target/Dockerfile
#docker cp  jenkins:/var/jenkins_home/jobs/java-admin/workspace/xingchen-admin/target/xingchen-admin.jar  /home/yangxy/workspace/java-admin/xingchen-admin/target/xingchen-admin.jar



set -eux
dockerServerName='dev_java-admin'
port='8088'
debugPort='49097'
debugCommand="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=49097"
maxHeapSize='800m'
TARGET_WORKSPACE='/home/xingchen/workspace/java-admin'

PROJECT_NAME=$(docker ps -a | grep ${dockerServerName} | awk '{print $1}');
if test -z "$PROJECT_NAME";
then
  echo "docker 没有 images ";
else
   echo "开始删除:$PROJECT_NAME ";
   docker rm -f $PROJECT_NAME
fi;


   ##删除旧的images
IMAGE_ID=$(docker images | grep "registry.cn-hangzhou.aliyuncs.com/yxy_dev/${dockerServerName} " | awk '{print $3}');
if test -z "$IMAGE_ID";
then
  echo "没有images";
else
   docker rmi ${IMAGE_ID};
fi;

cd ${TARGET_WORKSPACE}/xingchen-admin/target


docker build --build-arg JAR_FILE=xingchen-admin.jar -f ${TARGET_WORKSPACE}/xingchen-admin/target/Dockerfile -t registry.cn-hangzhou.aliyuncs.com/xingchen_1/${dockerServerName}:latest .

# 需要在服务器上执行登陆命令先，不然会被拒绝
# docker login --username=杨星耀 registry.cn-hangzhou.aliyuncs.com

docker push registry.cn-hangzhou.aliyuncs.com/xingchen_1/${dockerServerName}:latest

#docker pull registry.cn-hangzhou.aliyuncs.com/xingchen_1/dev_java-admin:latest

docker run --restart=always --security-opt seccomp=unconfined -m ${maxHeapSize} --add-host host.xingchen:172.17.0.1 --add-host mysql.xingchen:172.17.0.1 --add-host redis.xingchen:172.17.0.1 -v ~/logs/${dockerServerName}/:/root/logs/${dockerServerName} -d -e JVM_PARAM=${debugCommand} --name ${dockerServerName} -p ${port}:${port} -p ${debugPort}:${debugPort} --pid=host registry.cn-hangzhou.aliyuncs.com/xingchen_1/${dockerServerName}:latest


